package com.imooc.ecommerce.config;

import feign.Feign;
import okhttp3.ConnectionPool;
import okhttp3.OkHttpClient;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.cloud.openfeign.FeignAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.TimeUnit;

/**
 * openfeign 使用 okhttp 配置类
 * @Author: zhangshao
 * @Since: 2023/11/29 17:41
 */
@Configuration
@ConditionalOnClass(Feign.class)
@AutoConfigureBefore(FeignAutoConfiguration.class)
public class FeignOkHttpConfig {

	@Bean
	public okhttp3.OkHttpClient okHttpClient(){
		return new OkHttpClient.Builder()
				//设置连接超时
				.connectTimeout(5, TimeUnit.SECONDS)
				//设置读超时
				.readTimeout(5,TimeUnit.SECONDS)
				//设置写超时
				.writeTimeout(5,TimeUnit.SECONDS)
				//是否自动重连
				.retryOnConnectionFailure(true)
				//配置连接池中的最大空闲线程个数为 10, 并保持5分钟
				.connectionPool(new ConnectionPool(10,5L,TimeUnit.MINUTES))
				.build();
	}

}
